/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.product.info;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.entity.InfoType;
import org.hutech09.jsale.entity.ProductDetail;

/**
 *
 * @author Nam
 */
@ManagedBean
@RequestScoped
public class PropertyAddingCtrl {

    private String txtPropertyName;

    private String propertyValue;

    @ManagedProperty("#{propertyCtrl}")
    private PropertyCtrl propertyCtrl;

    @EJB
    private IProductManager pm;

    public PropertyAddingCtrl() {
    }

    public String getTxtPropertyName() {
        return txtPropertyName;
    }

    public void setTxtPropertyName(String txtPropertyName) {
        this.txtPropertyName = txtPropertyName;
    }

    public String getPropertyValue() {
        return propertyValue;
    }

    public void setPropertyValue(String propertyValue) {
        this.propertyValue = propertyValue;
    }

    public PropertyCtrl getPropertyCtrl() {
        return propertyCtrl;
    }

    public void setPropertyCtrl(PropertyCtrl propertyCtrl) {
        this.propertyCtrl = propertyCtrl;
    }

    public List<String> complete(String str) {
        List<String> result = new ArrayList<String>();
        for (InfoType info : pm.findListInfoType(str)) {
            result.add(info.getName());
        }
        return result;
    }

    public void create() {
        txtPropertyName = txtPropertyName.trim();
        if(txtPropertyName.length()==0)
            return;
        InfoType infoType = pm.findInfoType(txtPropertyName);
        if (infoType == null) {
            infoType = new InfoType();
            infoType.setName(txtPropertyName);
            infoType = pm.createInfoType(infoType);
        }
        ProductDetail detail = new ProductDetail();
        detail.setInfoType(infoType);
        detail.setValue(propertyValue);
        detail.setProduct(propertyCtrl.getProduct());
        if (propertyCtrl.getProduct() == null) {
            return;
        }
        pm.addDetail(detail);
        propertyCtrl.getAllProperties();
        txtPropertyName = propertyValue = "";
    }
    
}
